layui.use(['layer', 'form', 'ztree', 'laydate', 'admin', 'ax', 'table', 'treetable'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var table = layui.table;
    var treetable = layui.treetable;


    var SysMenuModule = { tableId: "SysMenuModuleTable" };

    //初始化左侧部门树
    var ztree = new $ZTree("menuTree", "/menu/selectMenuTreeList");
    ztree.bindOnClick(SysMenuModule.onClickMenu);
    ztree.init();

    SysMenuModule.onClickMenu = function (e, treeId, treeNode) {
        SysMenuModule.condition.menuId = treeNode.id;
        SysMenuModule.search();
    };

    SysMenuModule.initTable = function (menuId, data) {
        return treetable.render({
            elem: '#' + menuId,
            url: Feng.ctxPath + '/menu/listTree',
            where: data,
            page: false,
            cellMinWidth: 100,
            toolbar: '#toolbarDemo',
            loading: true,
            request: { pageName: 'current', limitName: 'size'},
            // response: { statusName: 'code' ,statusCode: 200 ,msgName: 'message' ,countName: 'total' ,dataName: 'records' },
            parseData: function(res){
                return {
                    "code": 0, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            },
            cols: [[
                {type: 'numbers',title: '序号'},
                {field: 'menuId', hide: true,  title: 'id'},
                {field: 'name',  title: '菜单名称',minWidth: 200},
                {field: 'code',  title: '菜单编号'},
                {field: 'pcode',  title: '菜单父编号'},
                {field: 'url',  title: '请求地址'},
                {field: 'sort',  title: '排序'},
                {field: 'levels',  title: '层级'},
                {field: 'isMenuName',  title: '是否是菜单'},
                {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
            ]],
            treeColIndex: 2,
            treeSpid: "0",
            treeIdName: 'code',
            treePidName: 'pcode',
            treeDefaultClose: false,
            treeLinkage: true
        });
    };

    table.on('tool(' + SysMenuModule.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'btnEdit') {
            admin.putTempData('formOk', false);
            top.layui.admin.open({
                type: 2,
                title: '编辑菜单',
                content: Feng.ctxPath + '/menu/menu_edit?menuId=' + data.menuId,
                end: function () {
                    admin.getTempData('formOk') && SysMenuModule.initTable(SysMenuModule.tableId);
                }
            });
        } else if (layEvent === 'btnDelete') {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/menu/remove", function () {
                    Feng.success("删除成功!");
                    SysMenuModule.condition.menuId = "";
                    SysMenuModule.initTable(SysMenuModule.tableId);
                }, function (res) {
                    Feng.error("删除失败!" + res.responseJSON.message + "!");
                });
                ajax.set("menuId", data.menuId);
                ajax.start();
            };
            Feng.confirm("是否删除菜单" + data.name + "?", operation);
        }
    });

    //////////////////////////搜索////////////////////////////
    $('#btnSearch').click(function () {
        SysMenuModule.search();
    });
    SysMenuModule.search = function () {
        var queryData = {};
        queryData['menuName'] = $("#menuName").val();
        queryData['level'] = $("#level").val();
        SysMenuModule.initTable(SysMenuModule.tableId, queryData);
    };

    ////////////////////////////添加//////////////////////
    $('#btnAdd').click(function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加菜单',
            content: Feng.ctxPath + '/menu/menu_add',
            end: function () {
                admin.getTempData('formOk') && SysMenuModule.initTable(SysMenuModule.tableId);
            }
        });
    });

    ////////////////////////////折叠展开////////////////////
    var tableResult = SysMenuModule.initTable(SysMenuModule.tableId);
    $('#expandAll').click(function () {
        treetable.expandAll('#' + SysMenuModule.tableId);
    });
    $('#foldAll').click(function () {
        treetable.foldAll('#' + SysMenuModule.tableId);
    });



});
